package com.ruoyi.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dyun.tool.mybatis.domain.PageDomain;
import com.ruoyi.common.annotation.SysLog;
import com.ruoyi.common.enums.BusinessTypeEnum;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.entity.SysNotice;
import com.ruoyi.project.system.service.SysNoticeService;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.core.result.R;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.core.validation.CreateGroup;
import net.dreamlu.mica.core.validation.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 公告 控制层
 *
 * @author dy
 */
@RestController
@RequestMapping("/system/notice")
@AllArgsConstructor
public class SysNoticeController extends BaseController {

    private final SysNoticeService sysNoticeService;

    /**
     * 分页
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public R<IPage<SysNotice>> list(SysNotice sysNotice, PageDomain pageDomain) {
        IPage<SysNotice> pages = sysNoticeService.page(startPage(pageDomain), new LambdaQueryWrapper<SysNotice>()
                .like($.isNotBlank(sysNotice.getNoticeTitle()), SysNotice::getNoticeTitle, sysNotice.getNoticeTitle())
                .eq($.isNotBlank(sysNotice.getNoticeType()), SysNotice::getNoticeType, sysNotice.getNoticeType())
                .like($.isNotBlank(sysNotice.getCreateBy()), SysNotice::getCreateBy, sysNotice.getCreateBy()));
        return success(pages);
    }

    /**
     * 详情
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping("/detail")
    public R<SysNotice> detail(Long id) {
        return success(sysNoticeService.getById(id));
    }

    /**
     * 新增
     */
    @SysLog(title = "通知公告", businessType = BusinessTypeEnum.INSERT)
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @PostMapping("/add")
    public R add(@Validated(value = {CreateGroup.class}) @RequestBody SysNotice sysNotice) {
        return status(sysNoticeService.saveIgnore(sysNotice));
    }

    /**
     * 修改
     */
    @SysLog(title = "通知公告", businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @PutMapping("/update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody SysNotice sysNotice) {
        return status(sysNoticeService.updateById(sysNotice));
    }

    /**
     * 删除
     */
    @SysLog(title = "通知公告", businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable Long[] ids) {
        List<SysNotice> sysNotices = new ArrayList<>();
        List<Long> idList = new ArrayList<>(Arrays.asList(ids));
        idList.forEach(v -> {
            SysNotice sysNotice = new SysNotice();
            sysNotice.setId(v);
            sysNotices.add(sysNotice);
        });
        boolean result = sysNoticeService.updateBatchById(sysNotices) && sysNoticeService.removeByIds(idList);
        return status(result);
    }
}